package jee.boot;

import jee.boot.common.basic.ProjectBasicInfo;
import jee.boot.common.utils.SpringContextHolder;
import jee.boot.module.iexp.config.ThreadPoolConfig;
import jee.boot.module.iexp.task.TaskListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;

import java.util.TimeZone;


@EnableCaching
@SpringBootApplication/*如果不开启工作流服务,请注释掉这行代码*///(exclude = { org.activiti.spring.boot.SecurityAutoConfiguration.class })
public class SingletonServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SingletonServiceApplication.class, args);
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        ProjectBasicInfo.printProjectBasicInfo();
        serviceStatusMonitoring();
    }

    /**
     * 服务状态监控
     */
    private static void serviceStatusMonitoring() {
        // 后台任务监听线程
        TaskListener taskListener = SpringContextHolder.getApplicationContext().getBean(TaskListener.class);
        ThreadPoolConfig poolConfig = SpringContextHolder.getApplicationContext().getBean(ThreadPoolConfig.class);

        /**
         * 监控后台任务服务线程健康状态,如果意外停止,自动重启
         */
        while (true) {
            // 自动监听[后台任务监听线程],如果[后台任务监听线程]因为业务出现异常导致死亡,那么会在10s后自动重启[后台任务监听线程]
            if (!taskListener.isAlive() && poolConfig.getEnable()) {
                taskListener.listenTask();
            }

            // TODO 监控其他线程

            try {
                Thread.sleep(1000 * 10);//检测后台任务监听线程间隔时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

